<?php

require_once "database/types/Article.php";
require_once "database/types/Section.php";

require_once "smarty/SmartyPaginate.class.php";

require_once "ACBController.php";
require_once "database/DatabaseController.php";

/**
 * @author A.J. Janssen
 */
class SectionController extends ACBController
{   
    //Page data
    /** @var Section */
    private $section;
    private $sectionExists = false;
    private $article;
    private $articleExists = false;
    
    //Tree data
    private $categories;
    private $articles;
    
    function __construct()
    {
        parent::__construct();
        $this->checkSection();
        $this->checkArticle();
        $this->collectTreeData();
        $this->updateACBSmarty();
    }
    
    public function validSection()
    {
        return !empty($this->section);
    }
    
    public function getSectionName()
    {
        return $this->section->name;
    }
    
    public function getSectionId()
    {
        return $this->section->id;
    }
    
    private function checkSection()
    {
        if (!empty($_GET['section']))
        {
            $section = $this->dbc->getSection($_GET['section']);
            if (empty($section->name))
                return;
            $this->sectionExists = true;
            $this->section = $section;
        }
    }
    
    private function checkArticle()
    {
        if (!empty($_GET['article']))
        {
            $article = $this->dbc->getArticle($_GET['article']);
            if (!empty($article->created))
            {
                $this->articleExists = true;
                $this->article = $article;
            }
        }
    }
    
    private function collectTreeData()
    {
        //Get articles
        $this->articles = $this->dbc->getBasicInfoArticles();
        $this->categories = $this->dbc->getAllCategories();
        //If current section is not knowledgebase, filter categories and articles
        if ($this->section->id != 4)
        {
             //Filter out non-assigned categories
            $this->filterCategories();
            //Now that the categories have been filtered, filter the articles
            $this->filterArticles();
        }
        //Remove empty categories
        $this->removeEmptyCategories();
    }
    
    private function filterCategories()
    {
        //Get all main categories not assigned to current section
        $unassignedCategories = $this->dbc->getUnassignedMainCategories($this->section->id);
        //Get array difference (this fragment is basically a simplified 'array_diff' call)
        foreach ($unassignedCategories as $category)
        {
            unset($this->categories[$category->id]);
        }
        //Start filtering (iterates through values, looking for elements without parents and removes them, stops after 1 run without filterings)
        do
        {
            $filteredSomething = false;
            foreach ($this->categories as $category)
            {
                if ($category->parentid != 0 && !isset($this->categories[$category->parentid]))
                {
                    unset($this->categories[$category->id]);
                    $filteredSomething = true;
                }
            }
        } while($filteredSomething);
    }
    
    private function filterArticles()
    {
        //Assumes categories have already been filtered
        foreach ($this->articles as $article)
        {
            if (!isset($this->categories[$article->parentid]))
                unset($this->articles[$article->id]);
        }
    }
    
    private function removeEmptyCategories()
    {
        $emptyCategories = $this->getEmptyCategories($this->categories, $this->articles);
        while (!empty($emptyCategories))
        {
            foreach ($emptyCategories as $emptyCategory)
                unset($this->categories[$emptyCategory]);
            $emptyCategories = $this->getEmptyCategories($this->categories, $this->articles);
        }
    }
    
    private function getEmptyCategories($categories, $articles)
    {
        $parents = array();
        foreach ($categories as $category)
            if ($category->parentid != 0)
                $parents[] = $category->parentid;
        foreach ($articles as $article)
            $parents[] = $article->parentid;
        $parents = array_unique($parents);
        $emptyCategories = array();
        foreach ($categories as $category)
            if (!in_array($category->id, $parents))
                $emptyCategories[] = $category->id;
        return $emptyCategories;
    }
    
    private function updateACBSmarty()
    {
        if (!$this->sectionExists)
            return;
        $this->acb->assign('sectionCss', $this->section->stylesheet);
        $this->acb->assign('sectionName', $this->section->name);
        $this->acb->assign('sectionId', $this->section->id);
        $this->acb->assign('sectionDescription', $this->section->description);
        //Sort and filter categories before assinging
        usort($this->categories, "cmpCategory");
        $this->acb->assign('categories', $this->categories);
        //Sort articles before assigning
        usort($this->articles, "cmpArticle");
        $this->acb->assign('articles', $this->articles);
        if (!$this->articleExists)
            return;
        $this->acb->assign('currentArticle', $this->article);
    }
}

?>
